<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 会员等级类
 * @package  module/base/customer
 * @author   allen.qiang
 * @version 1.0
 */

class custAccount {

    /**
     * 查询会员账户科目
     * @param array $args_array
     * @return array|null
     */
    public function search( &$args_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();


        //会员号
        if (isset($args_array['pcustID']) && quanli_is_int($args_array['pcustID'])) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 'i';
            $params_array[] = $args_array['pcustID'];
        }

        //账户科目ID
        if (isset($args_array['subjectId']) && quanli_is_int($args_array['subjectId'])) {
            $sql .= ' AND subjectId = ?';
            $params_type .= 'i';
            $params_array[] = $args_array['subjectId'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($args_array['rp']) && $args_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM cust_account ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $args_array['count'] = 0;
                    return null;
                }

                $args_array['count'] = $item_count;

                //排序
                if (isset($args_array['sortname']) && !empty($args_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $args_array['sortname'], $args_array['sortorder']);

                //分页
                if (!isset($args_array['page']) || !is_numeric($args_array['page']) || $args_array['page'] <= 0)
                    $args_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($args_array['page'] - 1) * $args_array['rp'], $args_array['rp']);

            } else {

                //排序
                if (isset($args_array['sortname']) && !empty($args_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $args_array['sortname'], $args_array['sortorder']);

            }

            //查询
            $search_sql = 'SELECT * FROM cust_account ' . $sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            $item_array = array();
            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'custID' => $item->custID,
                    'pcustID' => $item->pcustID,
                    'subjectId' => $item->subjectId,
                    'subjectName' => $item->subjectName,
                    'useableAccount' => $item->useableAccount,
                    'lockAccount' => $item->lockAccount,
                    'useableChangeTime' => $item->useableChangeTime,
                    'lockChangeTime' => $item->lockChangeTime,
                    'isLock' => $item->isLock,
                    'lockTimeStamp' => $item->lockTimeStamp
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
        
    }

    /**
     * 查询会员账户科目变更记录
     * @param array $args_array
     * @return array|null
     */
    public function searchLog( &$args_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();


        //会员号
        if (isset($args_array['pcustID']) && quanli_is_int($args_array['pcustID'])) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 'i';
            $params_array[] = $args_array['pcustID'];
        }

        //账户科目ID
        if (isset($args_array['subjectId']) && quanli_is_int($args_array['subjectId'])) {
            $sql .= ' AND subjectId = ?';
            $params_type .= 'i';
            $params_array[] = $args_array['subjectId'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($args_array['rp']) && $args_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM cust_account_log ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $args_array['count'] = 0;
                    return null;
                }

                $args_array['count'] = $item_count;

                //排序
                if (isset($args_array['sortname']) && !empty($args_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $args_array['sortname'], $args_array['sortorder']);

                //分页
                if (!isset($args_array['page']) || !is_numeric($args_array['page']) || $args_array['page'] <= 0)
                    $args_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($args_array['page'] - 1) * $args_array['rp'], $args_array['rp']);

            } else {

                //排序
                if (isset($args_array['sortname']) && !empty($args_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $args_array['sortname'], $args_array['sortorder']);

            }

            //查询
            $search_sql = 'SELECT * FROM cust_account_log ' . $sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            $item_array = array();

            $action_type_array = array(
                1 => '交易冻结',
                2 => '易冻结取消',
                3 => '交易冻结扣除',
                4 => '充值',
                5 => '消费',
                5 => '扣减',
                6 => '特殊调整',
                7 => '交易回滚取消'
            );

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'createTime' => $item->createTime,
                    'custID' => $item->custID,
                    'pcustID' => $item->pcustID,
                    'subjectId' => $item->subjectId,
                    'seqno' => $item->seqno,
                    'subjectName' => $item->subjectName,
                    'externalTranCode' => $item->externalTranCode,
                    'changeAccount' => $item->changeAccount,
                    'useableAccount' => $item->useableAccount,
                    'changeLockAccount' => $item->changeLockAccount,
                    'lockAccount' => $item->lockAccount,
                    'changeType' => $item->changeType,
                    'actionType' => $item->actionType,
                    'actionTypeName' => $action_type_array[$item->actionType],
                    'isRollback' => $item->isRollback,
                    'remark' => $item->remark
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 交易冻结
     * @param array $args_array
     * $args_array = array(
     *      'operator' => '操作人',
     *      'pcustID' => '会员号',
     *      'subjectId' => '科目ID',
     *      'amount' => '充值金额',
     *      'remark' => '备注',
     *      'externalTranCode' => '外部交易流水号'
     * )
     * @param bool $is_transaction 是否开启事务
     * @return array
     */
    public function accountBlock( $args_array, $is_transaction = false ){

        global $G_SHOP;

        if (!isset($args_array['operator'], $args_array['pcustID'], $args_array['subjectId'], $args_array['amount'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        if (!quanli_is_int($args_array['pcustID'])) {
            return array( 'code' => 400, 'message' => '会员号错误' );
        }

        if (!quanli_is_int($args_array['subjectId'])) {
            return array( 'code' => 400, 'message' => '科目ID错误' );
        }

        if (!is_numeric($args_array['amount']) || $args_array['amount'] <= 0) {
            return array( 'code' => 400, 'message' => '金额错误' );
        }

        if (!isset($args_array['remark']) || empty($args_array['remark'])) {
            $args_array['remark'] = null;
        }

        if (!isset($args_array['externalTranCode']) || empty($args_array['externalTranCode'])) {
            $args_array['externalTranCode'] = null;
        }

        try {

            if ($is_transaction) {
                $G_SHOP->DBCA->beginTransaction();
            }

            //更新或新建会员科目
            $subject_obj = new entity('cust_account');
            $subject_obj->pcustID = $args_array['pcustID'];
            $subject_obj->subjectId = $args_array['subjectId'];
            if (!$G_SHOP->DBCA->fetch($subject_obj)) {
                throw new Exception('没有会员账户科目，冻结失败', 404);
            }
            $subject_obj->useableAccount -= $args_array['amount'];
            $subject_obj->lockAccount += $args_array['amount'];
            $subject_obj->useableChangeTime = $G_SHOP->sys_datetime;
            $subject_obj->lockChangeTime = $G_SHOP->sys_datetime;
            $G_SHOP->DBCA->save($subject_obj);
            if($subject_obj->useableAccount < 0){
                throw new Exception('可用余额不足，冻结失败');
            }

            //记录科目变更日志
            $subject_log_obj = new entity('cust_account_log');
            $subject_log_obj->creator = $args_array['operator'];
            $subject_log_obj->custID = $subject_obj->custID;
            $subject_log_obj->pcustID = $args_array['pcustID'];
            $subject_log_obj->subjectId = $args_array['subjectId'];
            $subject_log_obj->seqno = generate_seqno('seqno_accout_log');
            $subject_log_obj->subjectName = $subject_obj->subjectName;
            $subject_log_obj->externalTranCode = $args_array['externalTranCode'];
            $subject_log_obj->changeAccount = $args_array['amount'];
            $subject_log_obj->useableAccount = $subject_obj->useableAccount;
            $subject_log_obj->changeLockAccount = $args_array['amount'];
            $subject_log_obj->lockAccount = $subject_obj->lockAccount;
            $subject_log_obj->changeType = 2;
            $subject_log_obj->actionType = 1;
            $subject_log_obj->remark = $args_array['remark'];
            $G_SHOP->DBCA->save( $subject_log_obj );

            if ($is_transaction) {
                $G_SHOP->DBCA->commit();
            }

            return array( 'code' => 200, 'message' => 'ok' );

        } catch (Exception $e) {

            if ($is_transaction) {
                $G_SHOP->DBCA->rollBack();
            }

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

    /**
     * 冻结回滚
     * @param array $args_array
     * $args_array = array(
     *      'operator' => '操作人',
     *      'pcustID' => '会员号',
     *      'subjectId' => '科目ID',
     *      'amount' => '充值金额',
     *      'remark' => '备注',
     *      'externalTranCode' => '外部交易流水号'
     * )
     * @param bool $is_transaction 是否开启事务
     * @return array
     */
    public function blockRollBack( $args_array, $is_transaction = false ){

        global $G_SHOP;

        if (!isset($args_array['operator'], $args_array['pcustID'], $args_array['subjectId'], $args_array['amount'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        if (!quanli_is_int($args_array['pcustID'])) {
            return array( 'code' => 400, 'message' => '会员号错误' );
        }

        if (!quanli_is_int($args_array['subjectId'])) {
            return array( 'code' => 400, 'message' => '科目ID错误' );
        }

        if (!is_numeric($args_array['amount']) || $args_array['amount'] <= 0) {
            return array( 'code' => 400, 'message' => '金额错误' );
        }

        if (!isset($args_array['remark']) || empty($args_array['remark'])) {
            $args_array['remark'] = null;
        }

        if (!isset($args_array['externalTranCode']) || empty($args_array['externalTranCode'])) {
            $args_array['externalTranCode'] = null;
        }

        try {

            if ($is_transaction) {
                $G_SHOP->DBCA->beginTransaction();
            }

            //更新或新建会员科目
            $subject_obj = new entity('cust_account');
            $subject_obj->pcustID = $args_array['pcustID'];
            $subject_obj->subjectId = $args_array['subjectId'];
            if (!$G_SHOP->DBCA->fetch($subject_obj)) {
                throw new Exception('没有会员账户科目，冻结回滚失败', 404);
            }
            $subject_obj->useableAccount += $args_array['amount'];
            $subject_obj->lockAccount -= $args_array['amount'];
            $subject_obj->useableChangeTime = $G_SHOP->sys_datetime;
            $subject_obj->lockChangeTime = $G_SHOP->sys_datetime;
            $G_SHOP->DBCA->save($subject_obj);
            if($subject_obj->lockAccount < 0){
                throw new Exception('冻结金额不足，冻结回滚失败', 404);
            }

            //记录科目变更日志
            $subject_log_obj = new entity('cust_account_log');
            $subject_log_obj->creator = $args_array['operator'];
            $subject_log_obj->custID = $subject_obj->custID;
            $subject_log_obj->pcustID = $args_array['pcustID'];
            $subject_log_obj->subjectId = $args_array['subjectId'];
            $subject_log_obj->seqno = generate_seqno('seqno_accout_log');
            $subject_log_obj->subjectName = $subject_obj->subjectName;
            $subject_log_obj->externalTranCode = $args_array['externalTranCode'];
            $subject_log_obj->changeAccount = $args_array['amount'];
            $subject_log_obj->useableAccount = $subject_obj->useableAccount;
            $subject_log_obj->changeLockAccount = $args_array['amount'];
            $subject_log_obj->lockAccount = $subject_obj->lockAccount;
            $subject_log_obj->changeType = 1;
            $subject_log_obj->actionType = 2;
            $subject_log_obj->remark = $args_array['remark'];
            $G_SHOP->DBCA->save( $subject_log_obj );

            if ($is_transaction) {
                $G_SHOP->DBCA->commit();
            }

            return array( 'code' => 200, 'message' => 'ok' );

        } catch (Exception $e) {

            if ($is_transaction) {
                $G_SHOP->DBCA->rollBack();
            }

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

    /**
     * 冻结扣减
     * @param array $args_array
     * $args_array = array(
     *      'operator' => '操作人',
     *      'pcustID' => '会员号',
     *      'subjectId' => '科目ID',
     *      'amount' => '充值金额',
     *      'remark' => '备注',
     *      'externalTranCode' => '外部交易流水号'
     * )
     * @param bool $is_transaction 是否开启事务
     * @return array
     */
    public function blockDeduction( $args_array, $is_transaction = false ){

        global $G_SHOP;

        if (!isset($args_array['operator'], $args_array['pcustID'], $args_array['subjectId'], $args_array['amount'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        if (!quanli_is_int($args_array['pcustID'])) {
            return array( 'code' => 400, 'message' => '会员号错误' );
        }

        if (!quanli_is_int($args_array['subjectId'])) {
            return array( 'code' => 400, 'message' => '科目ID错误' );
        }

        if (!is_numeric($args_array['amount']) || $args_array['amount'] <= 0) {
            return array( 'code' => 400, 'message' => '金额错误' );
        }

        if (!isset($args_array['remark']) || empty($args_array['remark'])) {
            $args_array['remark'] = null;
        }

        if (!isset($args_array['externalTranCode']) || empty($args_array['externalTranCode'])) {
            $args_array['externalTranCode'] = null;
        }

        try {

            if ($is_transaction) {
                $G_SHOP->DBCA->beginTransaction();
            }

            //更新或新建会员科目
            $subject_obj = new entity('cust_account');
            $subject_obj->pcustID = $args_array['pcustID'];
            $subject_obj->subjectId = $args_array['subjectId'];
            if (!$G_SHOP->DBCA->fetch($subject_obj)) {
                throw new Exception('没有会员账户科目，冻结失败', 404);
            }
            $subject_obj->lockAccount -= $args_array['amount'];
            $subject_obj->lockChangeTime = $G_SHOP->sys_datetime;
            $G_SHOP->DBCA->save($subject_obj);
            if($subject_obj->lockAccount < 0) {
                throw new Exception('冻结金额不足，扣减失败', 404);
            }

            //记录科目变更日志
            $subject_log_obj = new entity('cust_account_log');
            $subject_log_obj->creator = $args_array['operator'];
            $subject_log_obj->custID = $subject_obj->custID;
            $subject_log_obj->pcustID = $args_array['pcustID'];
            $subject_log_obj->subjectId = $args_array['subjectId'];
            $subject_log_obj->seqno = generate_seqno('seqno_accout_log');
            $subject_log_obj->subjectName = $subject_obj->subjectName;
            $subject_log_obj->externalTranCode = $args_array['externalTranCode'];
            $subject_log_obj->changeAccount = 0;
            $subject_log_obj->useableAccount = $subject_obj->useableAccount;
            $subject_log_obj->changeLockAccount = $args_array['amount'];
            $subject_log_obj->lockAccount = $subject_obj->lockAccount;
            $subject_log_obj->changeType = 2;
            $subject_log_obj->actionType = 3;
            $subject_log_obj->remark = $args_array['remark'];
            $G_SHOP->DBCA->save( $subject_log_obj );

            if ($is_transaction) {
                $G_SHOP->DBCA->commit();
            }

            return array( 'code' => 200, 'message' => 'ok' );

        } catch (Exception $e) {

            if ($is_transaction) {
                $G_SHOP->DBCA->rollBack();
            }

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }


    /**
     * 账户充值
     * @param array $args_array
     * $args_array = array(
     *      'operator' => '操作人',
     *      'pcustID' => '会员号',
     *      'subjectId' => '科目ID',
     *      'amount' => '充值金额',
     *      'remark' => '备注',
     *      'externalTranCode' => '外部交易流水号'
     * )
     * @param bool $is_transaction 是否开启事务
     * @return array
     */
    public function recharge( $args_array, $is_transaction = false ) {

        global $G_SHOP;

        if (!isset($args_array['operator'], $args_array['pcustID'], $args_array['subjectId'], $args_array['amount'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        if (!quanli_is_int($args_array['pcustID']) || $args_array['pcustID'] <= 0) {
            return array( 'code' => 400, 'message' => '会员号错误' );
        }

        if (!quanli_is_int($args_array['subjectId']) || $args_array['subjectId'] <= 0) {
            return array( 'code' => 400, 'message' => '科目ID错误' );
        }

        if (!is_numeric($args_array['amount']) || $args_array['amount'] <= 0) {
            return array( 'code' => 400, 'message' => '金额错误' );
        }

        if (!isset($args_array['remark']) || empty($args_array['remark'])) {
            $args_array['remark'] = null;
        }

        if (!isset($args_array['externalTranCode']) || empty($args_array['externalTranCode'])) {
            $args_array['externalTranCode'] = null;
        }

        try {

            if ($is_transaction) {
                $G_SHOP->DBCA->beginTransaction();
            }


            //更新或新建会员科目
            $subject_obj = new entity('cust_account');
            $subject_obj->pcustID = $args_array['pcustID'];
            $subject_obj->subjectId = $args_array['subjectId'];
            if (!$G_SHOP->DBCA->fetch($subject_obj)) {

                //加载类文件
                quanli_include('module/base/customer/quanliCust.class.php');
                quanli_include('module/base/system/sysPayment.class.php');

                //实例会员类
                $quanliCust = new quanliCust( $G_SHOP->DBCA );

                //实例支付方式类
                $sysPayment = new sysPayment();

                //查询会员
                $cust_args_array = array(
                    'pcustID' => $args_array['pcustID']
                );
                $cust_array = $quanliCust->getCustomerInfo( $cust_args_array );
                if (!$cust_array) {
                    throw new Exception('会员信息没有找到', 404);
                }

                //查询支付方式
                $pay_type_array = $sysPayment->searchPayTypeDetail( $args_array['subjectId'] );
                if (!$pay_type_array) {
                    throw new Exception('支付方式信息没有找到', 404);
                }


                $subject_obj->custID = $cust_array[0]['custID'];
                $subject_obj->creator = $args_array['operator'];
                $subject_obj->useableAccount = $args_array['amount'];
                $subject_obj->subjectName = $pay_type_array['payTypeName'];

            } else {

                $subject_obj->useableAccount += $args_array['amount'];

            }
            $subject_obj->useableChangeTime = $G_SHOP->sys_datetime;
            $G_SHOP->DBCA->save($subject_obj);


            //记录科目变更日志
            $subject_log_obj = new entity('cust_account_log');
            $subject_log_obj->creator = $args_array['operator'];
            $subject_log_obj->custID = $subject_obj->custID;
            $subject_log_obj->pcustID = $args_array['pcustID'];
            $subject_log_obj->subjectId = $args_array['subjectId'];
            $subject_log_obj->seqno = generate_seqno('seqno_accout_log');
            $subject_log_obj->subjectName = $subject_obj->subjectName;
            $subject_log_obj->externalTranCode = $args_array['externalTranCode'];
            $subject_log_obj->changeAccount = $args_array['amount'];
            $subject_log_obj->useableAccount = $subject_obj->useableAccount;
            $subject_log_obj->changeLockAccount = 0;
            $subject_log_obj->lockAccount = $subject_obj->lockAccount;
            $subject_log_obj->changeType = 1;
            $subject_log_obj->actionType = 4;
            $subject_log_obj->remark = $args_array['remark'];
            $G_SHOP->DBCA->save( $subject_log_obj );

            if ($is_transaction) {
                $G_SHOP->DBCA->commit();
            }

            return array( 'code' => 200, 'message' => 'ok' );

        } catch (Exception $e) {

            if ($is_transaction) {
                $G_SHOP->DBCA->rollBack();
            }

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }
    /**
     * 账户扣减/消费
     * @param array $args_array 充值参数
     * $args_array = array(
     *      'operator' => '操作人',
     *      'pcustID' => '会员号',
     *      'subjectId' => '科目ID',
     *      'amount' => '扣减金额',
     *      'remark' => '备注',
     *      'externalTranCode' => '外部交易流水号'
     * )
     * @param bool $is_transaction 是否开启事务
     * @return array
     */
    public function deduction( $args_array, $is_transaction = false ) {

        global $G_SHOP;

        if (!isset($args_array['operator'], $args_array['pcustID'], $args_array['subjectId'], $args_array['amount'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        if (!quanli_is_int($args_array['pcustID'])) {
            return array( 'code' => 400, 'message' => '会员号错误' );
        }

        if (!quanli_is_int($args_array['subjectId'])) {
            return array( 'code' => 400, 'message' => '科目ID错误' );
        }

        if (!is_numeric($args_array['amount']) || $args_array['amount'] <= 0) {
            return array( 'code' => 400, 'message' => '金额错误' );
        }

        if (!isset($args_array['remark']) || empty($args_array['remark'])) {
            $args_array['remark'] = null;
        }

        if (!isset($args_array['externalTranCode']) || empty($args_array['externalTranCode'])) {
            $args_array['externalTranCode'] = null;
        }


        try {

            if ($is_transaction) {
                $G_SHOP->DBCA->beginTransaction();
            }

            //更新或新建会员科目
            $subject_obj = new entity('cust_account');
            $subject_obj->pcustID = $args_array['pcustID'];
            $subject_obj->subjectId = $args_array['subjectId'];
            if (!$G_SHOP->DBCA->fetch($subject_obj)) {
                throw new Exception('会员账户科目没有找到，扣款失败', 404);
            }
            $subject_obj->useableAccount -= $args_array['amount'];
            $subject_obj->useableChangeTime = $G_SHOP->sys_datetime;
            $G_SHOP->DBCA->save($subject_obj);
            if ($subject_obj->useableAccount < 0) {
                throw new Exception($subject_obj->subjectName . '账户不足，扣款失败', 404);
            }


            //记录科目变更日志
            $subject_log_obj = new entity('cust_account_log');
            $subject_log_obj->creator = $args_array['operator'];
            $subject_log_obj->custID = $subject_obj->custID;
            $subject_log_obj->pcustID = $subject_obj->pcustID;
            $subject_log_obj->subjectId = $subject_obj->subjectId;
            $subject_log_obj->seqno = generate_seqno('seqno_accout_log');
            $subject_log_obj->subjectName = $subject_obj->subjectName;
            $subject_log_obj->externalTranCode = $args_array['externalTranCode'];
            $subject_log_obj->changeAccount = $args_array['amount'];
            $subject_log_obj->useableAccount = $subject_obj->useableAccount;
            $subject_log_obj->changeLockAccount = 0;
            $subject_log_obj->lockAccount = $subject_obj->lockAccount;
            $subject_log_obj->changeType = 2;
            $subject_log_obj->actionType = 5;
            $G_SHOP->DBCA->save( $subject_log_obj );

            if ($is_transaction) {
                $G_SHOP->DBCA->commit();
            }

            return array( 'code' => 200, 'message' => 'ok' );

        } catch (Exception $e) {

            if ($is_transaction) {
                $G_SHOP->DBCA->rollBack();
            }

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

}